home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 7.2 KB | 287 lines |
- package symantec.itools.multimedia;
-
-
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.MediaTracker;
- import java.awt.Toolkit;
- import java.net.URL;
- import java.net.MalformedURLException;
-
- // 01/29/97 TWB Integrated changes from Windows
- // 02/19/97 RKM Changed default for centerMode to correspond with DES file
- // Changed setCenterMode to call repaint instead of invalidate
- // Changed setCenterMode to repaint only if value had changed
-
- /**
- * ImageViewer component. Provides a platform-independent display of an Image.
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
-
- public class ImageViewer
- extends Canvas
- {
- /**
- * Image that this viewer is displaying.
- */
- protected Image image;
-
- /**
- * Name of file, if any, associated with this image.
- */
- protected String fileName;
-
- /**
- * URL of the image being displayed.
- */
- protected URL url;
-
- /**
- * Center image within component display area.
- */
- protected boolean centerMode;
-
- /**
- * Create default image viewer.
- */
- public ImageViewer()
- {
- image = null;
- fileName = null;
- url = null;
- centerMode = true;
- }
-
- /**
- * Crate image viewer with filename. The specified filename is used as
- * the image source.
- *
- * @param str name of file containing the image source
- * @exception MalformedURLException Thrown if URL cannot be generated from filename
- *
- */
- public ImageViewer(String str)
- throws MalformedURLException
- {
- setFileName(str);
- }
-
- /**
- * Create image viewer with URL. The specified URL is used as
- * the image source.
- *
- * @param url the URL of the image to be displayed
- */
- public ImageViewer(URL url)
- {
- setURL(url);
- }
-
- /**
- * Create image viewer with image. The specified image is used as
- * the image source
- *
- * @param img the image to be displayed
- */
- public ImageViewer(Image img)
- {
- setImage(img);
- }
-
- /**
- * Specify or change the image filename.
- *
- * @param str name of file containing image source
- *
- */
- public void setFileName(String str)
- {
- try
- {
- fileName = str;
- setURL(new URL(fileName));
- }
- catch(MalformedURLException e)
- {
- //System.out.println("malformed URL");
- }
- repaint();
- }
-
- /**
- * Obtain the filename associated with the current image.
- *
- * @return the name of the file, if any, associated with this image. If
- no file is associated with this image, returns null
- */
- public String getFileName()
- {
- return fileName;
- }
-
- /**
- * Specify or change the image URL.
- *
- * @param aUrl the URL of the image to be displayed
- *
- */
- public void setURL(URL aUrl)
- {
- url = aUrl;
- fileName = null;
-
- Image loadedImage = getToolkit().getImage(url);
- if (loadedImage != null) {
- setImage(loadedImage);
- repaint();
- }
- }
-
- /**
- * Obtain the URL associated with the current image. If the image
- * was specified by file name, or URL, it will have a URL which
- * indicates its source. Images created using the constructor
- * with an Image parameter will have no associated URL.
- *
- * @return the name of the URL, if any, associated with this image. If
- no URL is associated with this image, returns null
- */
- public URL getURL()
- {
- return url;
- }
-
- /**
- * Specify or change the image centering mode. Image may be centered, or
- * drawn at the top left of the component area.
- *
- * @param flag center the image when true
- */
- public void setCenterMode(boolean flag)
- {
- if (centerMode != flag)
- {
- centerMode = flag;
- repaint();
- }
- }
-
- /**
- * Obtain current image centering mode.
- *
- * @return returns true if image is centered in component area
- */
- public boolean getCenterMode()
- {
- return centerMode;
- }
-
- /**
- * Set or change the current image. Call this method if you want to
- * specify directly the image to display.
- *
- * @param img the image to be displayed
- */
- public void setImage(Image img)
- {
- fileName = null;
- image = img;
-
- if (img != null) {
- MediaTracker tracker;
-
- try
- {
- tracker = new MediaTracker(this);
- tracker.addImage(image, 0);
- tracker.waitForID(0);
- }
- catch(InterruptedException e)
- {
- }
- } else {
- repaint();
- }
- }
-
- /**
- * Obtain the image currently being displayed.
- *
- * @return the image currently displayed or null if no image
- */
- public Image getImage()
- {
- return image;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * The image is optionally centered depending on the centerMode flag.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- if (image != null) {
- Dimension dim = size();
- g.clipRect(0, 0, dim.width, dim.height);
-
- int x = 0;
- int y = 0;
-
- if (centerMode) {
- x += (dim.width - image.getWidth(this)) / 2;
- y += (dim.height - image.getHeight(this)) / 2;
- }
-
- g.drawImage(image, x, y, this);
- }
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @return If no image has been loaded, a dimension of 10 by 10 is returned.
- * If an image has been loaded, the height and width of the image
- * is returned.
- *
- * @see #minimumSize
- */
- public Dimension preferredSize()
- {
- if (image != null)
- return (new Dimension(image.getWidth(this), image.getHeight(this)));
- else
- return new Dimension(10, 10);
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * @return If no image has been loaded, a dimension of 10 by 10 is returned.
- * If an image has been loaded, the height and width of the image
- * is returned.
- * @see #preferredSize
- */
- public Dimension minimumSize()
- {
- return preferredSize();
- }
- }
-